package com.neusoft.Shixun.redisdao.impl;

import com.neusoft.Shixun.redisdao.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

// Dao层专用注解
@Repository
public class RedisDaoImpl implements RedisDao {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        ValueOperations vo = redisTemplate.opsForValue();
        vo.set(key, value);
    }

    @Override
    public String get(String key) {
        ValueOperations vo = redisTemplate.opsForValue();
        String val = (String) vo.get(key);
        return val;
    }

    @Override
    public void set(String key, String value, long time, TimeUnit timeUnit) {
        ValueOperations vo = redisTemplate.opsForValue();
        vo.set(key, value, time, timeUnit);
    }

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.opsForValue().get(key) != null;
    }
}
